package com.shire.shire_interface.service.marketing;

import com.shire.shire_interface.dao.marketing.WxGroupTaskDao;
import com.shire.shire_interface.dao.marketing.WxGroupTaskItemDao;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.marketing.WxGroupTask;
import com.shire.shire_interface.model.marketing.WxGroupTaskExample;
import com.shire.shire_interface.model.marketing.WxGroupTaskItem;
import com.shire.shire_interface.model.marketing.WxGroupTaskItemExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 作者:  sll
 * 时间:  2019/8/28 19:29
 */
@Service
public class WxGroupTaskService {

    @Resource
    private WxGroupTaskDao wxGroupTaskDao;
    @Resource
    private WxGroupTaskItemDao wxGroupTaskItemDao;

    public int insertSelective(WxGroupTask wxGroupTask) {

        return wxGroupTaskDao.insertSelective(wxGroupTask);
    }

    public List<WxGroupTask> selectByExample(WxGroupTaskExample wxGroupTaskExample) {
        return wxGroupTaskDao.selectByExample(wxGroupTaskExample);
    }

    public int updateByPrimaryKeySelective(WxGroupTask wxGroupTask) {
        return wxGroupTaskDao.updateByPrimaryKeySelective(wxGroupTask);
    }

    public int updateForNewMember(String groupNo, String wxId){
        WxGroupTaskExample taskExample = new WxGroupTaskExample();
        taskExample.createCriteria().andGroupnoEqualTo(groupNo);
        List<WxGroupTask> tasks = wxGroupTaskDao.selectByExample(taskExample);
        if (tasks.size() < 1){
            return 0;
        }
        WxGroupTask task = tasks.get(0);
        WxGroupTaskItem item = new WxGroupTaskItem();
        item.setCurstep(3);
        WxGroupTaskItemExample itemExample = new WxGroupTaskItemExample();
        itemExample.createCriteria().andTaskidEqualTo(task.getId()).andWxidEqualTo(wxId);
        return wxGroupTaskItemDao.updateByExampleSelective(item,itemExample);
    }

    public int updateGroupTaskItemByCmdId(WxCommand command, Map<String, Object> respContent){
        WxGroupTaskExample example = new WxGroupTaskExample();
        example.createCriteria().andGroupcmdidEqualTo(command.getId().intValue());
        List<WxGroupTask> groupTaskList = wxGroupTaskDao.selectByExample(example);
        if (groupTaskList.size() > 0){
            WxGroupTask task = groupTaskList.get(0);
            task.setGroupcmdstatus(command.getRststatus());
            if (respContent.containsKey("groupWxId")){
                task.setGroupno((String)respContent.get("groupWxId"));
            }
            if (respContent.containsKey("mems")){
                String mems = (String) respContent.get("mems");
                List<String> members = new ArrayList<>();
                for (String s : mems.split(";")) {
                   members.add(s);
                }

                task.setFinishcount(members.size());

                WxGroupTaskItem item = new WxGroupTaskItem();
                item.setCurstep(3);
                item.setUpdatetime(new Date());
                WxGroupTaskItemExample itemExample = new WxGroupTaskItemExample();
                itemExample.createCriteria().andTaskidEqualTo(task.getId()).andWxidIn(members);
                wxGroupTaskItemDao.updateByExampleSelective(item, itemExample);
            }
            task.setUpdatetime(new Date());
            wxGroupTaskDao.updateByPrimaryKeySelective(task);

            return 1;
        }

        return -1;
    }


    public int updateForResult(Integer taskItemId){
        WxGroupTaskItem taskItem = wxGroupTaskItemDao.selectByPrimaryKey(taskItemId);

        //设置为好友
        taskItem.setCurstep(1);
        taskItem.setUpdatetime(new Date());

        return wxGroupTaskItemDao.updateByPrimaryKey(taskItem);
    }
}
